import {toComboBoxData} from "@/common/utils/page.util";

export const mixinPage = {

    data() {
        return {
            data: [],
            current: {},
            isCreate: true,
            formTitle: "",
            searchModel: {},
            configs: [],
            total: 0,
            page: 1,
            pagesize: 15,
            orders: "desc",
            layout: [
                "list",
                "links",
                "sep",
                "first",
                "prev",
                "next",
                "last",
                "sep",
                "refresh",
                "info"
            ],
        }
    },
    methods: {
        /**
         * 排序
         * @param field
         */
        order(field) {
            /*let val = this.orders[field]
            if(val === "asc") {
                this.orders[field] = "desc"
            } else {
                this.orders[field] = "asc"
            }
            */
            this.orders = "asc"
        },
        /**
         * 刷新页面
         */
        reload() {
            this.searchModel = {}
            this.page = 1
            this.search({})
            this.$refs.d1.close()
        },
        search: function (request) {

        },
        /**
         * 查询条件
         * @returns {*[]}
         */
        conditions: function () {
            return [
                // TODO 子类重写
            ]
        },
        /**
         * 提交查询
         */
        submitSearch: function () {
            this.search({pagesize: this.pagesize, conditions: this.conditions()})
        },
        /**
         * 分页提交
         * @param page
         */
        pageChange(page) {
            this.search({page: page.pageNumber, pagesize: page.pageSize, conditions: this.conditions()})
        },
        editEnd(row) {
            let field = row.column.field
            if (row.row[field] === row.originalValue) {
                return
            }
            let request = {
                editor: [
                    {"key": field, "value": row.row[field]}
                ],
                conditions: [
                    {"key": "id", "value": row.row["id"]}
                ]
            }
            this.submitEdit(request)
        },
        /**
         * 提交编辑
         * @param request
         */
        submitEdit(request) {

        },
        convertComboBoxData(object) {
            return toComboBoxData(object)
        },
        editCellCss(row, value) {
            return {background: "lightyellow"};
        },
        del(row) {
            this.$messager.confirm({
                title: "提示",
                msg: "确定删除?",
                result: r => {
                    if (r) {
                        let request = {
                            conditions: [
                                {key: "id", "value": row.id}
                            ]
                        }
                        this.submitDelete(request, row)
                    }
                }
            })
        },
        submitDelete(request, row) {
        },
        /**
         * 打开弹框
         * @param create
         * @param title
         * @param row
         */
        openForm(create, title, row) {
            this.$refs.d1.open()
            this.formTitle = title
            this.isCreate = create
            this.current = row
        }
    }

}